/*
 * Power.java
 * iRoomMonitor1.0
 * 11/23/10
 */

package RoomNanny.SpotSensors;

import com.sun.spot.peripheral.Spot;
import com.sun.spot.peripheral.IBattery;


/**
 * The point of this class is to implement a power-sensing feature that
 * would indicate the current percentage of the remaining power
 * in a sensor's battery. Ideally this would imply both
 * a reading being sent to the database and an LED reading on
 * the corresponding sensor.
 *
 * @author Tyler Mathison
 */
public class Power {


    //Creates an instance of battery for query purposes.
    private IBattery battery = Spot.getInstance().getPowerController().getBattery();
        

    /**
     * This method returns the number of LEDs to light given the
     * percent of power in the Sunspot.
     *
     * @param percent
     * @return int number of LEDsto light
     */
    public int powerDisplay(int percent){
        //Converts battery percentage into 8 lights worth
        //of a % bar. For use when displaying power.

        int ledVal=0;//Number of LED's to light up.

        if(percent >= 99)
        {
            ledVal = 8;
        }
        if((percent <= 98) && (percent >= 85)){
            ledVal = 7;
        }
        if((percent <= 84) && (percent >= 71)){
            ledVal = 6;
        }
        if((percent <= 70) && (percent >= 57)){
            ledVal = 5;
        }
        if((percent <= 56) && (percent >= 43)){
            ledVal = 4;
        }
        if((percent <= 42) && (percent >= 29)){
            ledVal = 3;
        }
        if((percent <= 28) && (percent >= 15)){
            ledVal = 2;
        }
        if((percent <= 14) && (percent >= 1)){
            ledVal = 1;
        }

        if(percent == 0){
            ledVal = 0;
        }

        return ledVal;

    }

    /**
     * This method gets the power level of the Sunspot, puts it into a number
     * between 0-8.
     *
     * @return returns the number of LEDs to light up.
     */
    public int getLedCount(){

        int powerLevel = battery.getBatteryLevel();
        //Powerlevel now has the percentage of power remaining

        int showPower = powerDisplay(powerLevel);
        //Showpower has the # of LEDs to light up.
        return showPower;

    }
    /**
     * This method returns the percent power of the battery.
     *
     * @return int percent power
     */
    public int getPowerLevel(){
        int powerLevel = battery.getBatteryLevel();
        return powerLevel;
    }
    /**
     * Approximation workaround since the percent returned by the Sunspot
     * class is not actually between 0-100.
     *
     * @return int approximated power percent
     */
    public int getPowerLevelBeta(){
        int powerLevel=getLedCount();
        powerLevel=powerLevel*12;
        return powerLevel;

    }




}
